import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useLocationStore = defineStore('location', {
  state: () => ({
    selectedLocation: {
      lng: null as number | null,
      lat: null as number | null,
      address: '',
      districtCode: null as string | null
    }
  }),
  actions: {
    setLocation(payload: { lng: number; lat: number; address?: string; districtCode?: string }) {
      this.selectedLocation = {
        lng: payload.lng,
        lat: payload.lat,
        address: payload.address || '',
        districtCode: payload.districtCode || ''
      }
    },
    clearLocation() {
      this.selectedLocation = { lng: null, lat: null, address: '', districtCode: '' }
    }
  },
  getters: {
    formattedLocation: (state) => {
      if (!state.selectedLocation.lng || !state.selectedLocation.lat) return '未选择位置'
      return state.selectedLocation
    }
  }
})
